<?php
if (!function_exists('ucenter_password_md5')) {
	/**
	 * 系统非常规MD5加密方法
	 * @param string $str 要加密的字符串
	 * @return string
	 * @version 1.0
	 * @author kangkai
	 */
	function ucenter_encrypt_md5(string $str, string $salt): string
	{
		return '' === $str ? '' : md5(sha1($str) . $salt);
	}
}


if (!function_exists('data_auth_sign')) {
	/**
	 * 数据签名认证
	 * @param array $data 被认证的数据
	 * @return string       签名
	 */
	function data_auth_sign($data)
	{
		//数据类型检测
		if (!is_array($data)) (array)$data;
		ksort($data); //排序
		$code = http_build_query($data); //url编码并生成query字符串
		$sign = sha1($code); //生成签名
		return $sign;
	}
}

if (!function_exists('list_to_tree')) {
	/**
	 * 把返回的数据集转换成Tree
	 * @param array $list 要转换的数据集
	 * @param string $pid parent标记字段
	 * @param string $level level标记字段
	 * @return array
	 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
	 */
	function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
	{
		// 创建Tree
		$tree = array();
		if (is_array($list)) {
			// 创建基于主键的数组引用
			$refer = array();
			foreach ($list as $key => $data) {
				$refer[$data[$pk]] =& $list[$key];
			}
			foreach ($list as $key => $data) {
				// 判断是否存在parent
				$parentId = $data[$pid];
				if ($root == $parentId) {
					$tree[] =& $list[$key];
				} else {
					if (isset($refer[$parentId])) {
						$parent =& $refer[$parentId];
						$parent[$child][] =& $list[$key];
					}
				}
			}
		}
		return $tree;
	}
}

if (!function_exists('tree_to_select')) {
	// tree转换为select
	function tree_to_select($list, $level = 0, $repeat = "├─")
	{
		$data = '';
		foreach ($list as $key => $value) {
			$data = $data . "<option value='" . $value['id'] . "'>" . str_repeat($repeat, $level) . $value['title'] . "</option>";
			if (!empty($value['_child'])) {
				$data = $data . tree_to_select($value['_child'], $level + 1);
			}
		}
		return $data;
	}
}

if (!function_exists('category_tree_to_view')) {
	function category_tree_to_view($list, $level = 0, $repeat = "├─")
	{
		$data = '';
		foreach ($list as $key => $value) {
			$data = $data . "<tr>
					<td>" . $value['id'] . " </td>
					<td>" . str_repeat($repeat, $level) . $value['title'] . "</td>
					<td>{$value['name']}</td>
					<td>{$value['image']}</td>
					<td><span>{$value['status']}</span></td>
					<td><span>{$value['lists_tpl']}</span></td>
					<td>{$value['detail_tpl']}</td>
					<td><a href=" . base_url('/bm/category/update/id/') . $value['id'] . " class=\"ajax-get\">修改</a>
						<a href=\"/index.php?s=/admin/auth_manager/group/uid/1.html\" class=\"confirm ajax-get\">删除</a>
					</td>
				</tr>";
			if (!empty($value['_child'])) {
				$data = $data . category_tree_to_view($value['_child'], $level + 1);
			}
		}
		return $data;
	}
}

/**
 * getTimeStamp('Y-m-d H:i:s:u')
 * 毫秒时间方法
 */
if (!function_exists('getTimeStamp')) {
	function getTimeStamp($format = 'Y-m-d H:i:s:u', $utimestamp = null)
	{
		if (is_null($utimestamp)) {
			$utimestamp = microtime(true);
		}

		$timestamp = floor($utimestamp);
		$milliseconds = round((1 + $utimestamp - $timestamp) * 1000);

		return date(preg_replace('`(?<!\\\\)u`', substr(strval($milliseconds), 1, 3), $format), $timestamp);
	}
}

if (!function_exists('createOrderNumber')) {
	/**
	 * 创建订单号
	 * @return string
	 */
	function createOrderNumber()
	{
		return getTimeStamp('YmdHisu') . str_pad(mt_rand(1, 999999), 6, '0', STR_PAD_LEFT);
	}
}


if (!function_exists('dd')) {
    /**
     * 断点格式化打印输出
     * @param array $array
     */
    function dd(array $array)
    {
        echo "<pre/>";
        print_r($array);
        exit;;
    }
}

